package com.softlook.mt.client.bricks.monitor;

import java.util.Date;
import java.util.Iterator;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.softlook.mt.client.data.Device;
import com.softlook.mt.client.data.DeviceCollection;
import com.softlook.mt.client.data.DeviceTrack;

public class MonitorServiceClnt {

	private DeviceCollection m_allDevices;
	private DeviceCollection m_chekedDevices;
	
	public MonitorServiceClnt(){
		DeviceTrack device1 = new DeviceTrack();
		DeviceTrack device2 = new DeviceTrack();
		DeviceTrack device3 = new DeviceTrack();
		DeviceTrack device4 = new DeviceTrack();
		
		device1.set(1,"test dev 1", 53.906, 27.578,
				"k~_hI{xhgDoMaOwVuWkPm[q^owA_Ect@eH}b@mf@wuCuc@s~Bk[s`BuPmq@{StPlC`O`JvIlC|LaJ|b@{@gCxAcH_DDaBWQe@",
				"BBBBBBBBBBBBBBBBBBBBB",
				"#00FF00");
		device2.set(2,"test dev 2", 53.907, 27.575,
				"{l`hIweigDjGoQgJ_NoKkSeJ`RvCnJsBlEaGaK",
				"BAB@AA@B",
				"#0000FF");
		device3.set(3,"test dev 3", 53.902, 27.572);
		device4.set(4,"test dev 4", 53.909, 27.574);

		m_allDevices = new DeviceCollection();
		m_allDevices.add(device1);
		m_allDevices.add(device2);
		m_allDevices.add(device3);
		m_allDevices.add(device4);
		
		
		m_chekedDevices = new DeviceCollection();
		m_chekedDevices.add(device1);
		m_chekedDevices.add(device2);
		m_chekedDevices.add(device3);
		m_chekedDevices.add(device4);
	}
	
	private void emulateDevicePosition(){
		final int k = 1000;
		Iterator deviceIterator = m_allDevices.iterator( );
		
		while (deviceIterator.hasNext( )) {
			
			Device device = (Device) deviceIterator.next();
			device.setPoint(device.getLat()+ (Math.random()-0.5)*2/k , device.getLng()+(Math.random()-0.5)*2/k);
		}
	}
	
	public void getAllDevices(AsyncCallback callback) {
		
		callback.onSuccess(m_allDevices);
	}
	
	public void getCheckedDevices(AsyncCallback callback){
		emulateDevicePosition();
		callback.onSuccess(m_chekedDevices);
	}

	public void getCheckedDeviceTracks(Date dateBegin, Date dateEnd, AsyncCallback callback){
		callback.onSuccess(m_chekedDevices);
	}
	
	public void checkDevice(Integer id, boolean isChecked, AsyncCallback callback){
		if (isChecked){
			Device device = m_allDevices.getDevice(id);
			m_chekedDevices.add(device);
		}else{
			m_chekedDevices.remove(id);
		}
	}

}
